<?php
	require_once './Tool/DB/SqlDBManager.class.php';
	$sqlDBManager= new SqlDBManager();

	$username = $_SESSION['username'];

	$sql = "SELECT * FROM attendancerecord
		WHERE DATE( SignInTimeStamp ) = CURDATE( ) 
		AND (SignOutTimeStamp is NULL 
		OR SignOutTimeStamp =  '0000-00-00 00:00:00')";
	$res=$sqlDBManager->queryRows($sql);

	if(!empty($res)){
		$sql = "SELECT * FROM attendancerecord 
			WHERE date(SignInTimeStamp)=CURDATE() 
			AND Status='Verified' 
			AND (SignOutTimeStamp is NULL 
			OR SignOutTimeStamp =  '0000-00-00 00:00:00') ";

		$res=$sqlDBManager->queryRows($sql);
		if(!empty($res)){
			$sql = "UPDATE attendancerecord 
				SET SignOutTimeStamp = NOW(),AuditSignOut=? 
				WHERE (SignOutTimeStamp is NULL 
				OR SignOutTimeStamp = '0000-00-00 00:00:00') 
				AND DATE( SignInTimeStamp ) = CURDATE() 
				AND Status=?";
				
			$parameters=array($username,'Verified');
			$res=$sqlDBManager->execute_dml($sql,$parameters);
			if($res>0){
				print '<script type="text/javascript">';
				print 'alert("Verified Attendance has been signed out.")';
				print '</script>';
				include_once("Attendance/getattendance.php");
				echo "<p></p>";
			}
		}else{
			print '<script type="text/javascript">';
			print 'alert("Please verify all attendance first.")';
			print '</script>';
			include_once("Attendance/getattendance.php");
			echo "<p></p>";
		}
	}else{
		print '<script type="text/javascript">';
		print 'alert("All attendance for today has already been signed out.")';
		print '</script>';
		include_once("Attendance/getattendance.php");
		echo "<p></p>";
	}
?>